********************************
** Read in file, add controls **

import delim using 	"./input/mayoral/city_mayors_filledgaps.csv", clear

tab party
replace party = "U" if party==""

gen 	f_myr = ym(y1, m1)
*drop year month

xtset	gen_id f_myr
tsfill	

bys gen_id (f_myr): carryforward city mayor party, replace

tab party
drop month year m1 y1
egen iparty = group(party)

tempfile parties
save "`parties'", replace

**********************

use 	"./output/cities_expandedpatents", clear
merge 1:1 gen_id f_myr using "`parties'"


** npi X month-of-year FEs **
egen	idxm = group(gen_id f_m)

** census region X year FEs **
egen	regyr = group(f_yr region)

keep if tin(1916m1, 1920m12)
tab _merge

drop _merge
*=======================*
*= Treatment variables  *
*=======================*

tab 	days_npis if f_myr==tm(1918m9)

gen		daysnpis = days_npis/30 
gen		lnnpis = ln(days_npis)
gen		longnpis = (days_npis>=90) if !mi(days_npis) // Natural break in data, about 61% have short
gen		ldeaths = ln(mag_maxexcessdeaths)

foreach v of varlist daysnpis longnpis lnnpis ldeaths {
	gen 	post_X_`v' 		= `v'*(f_myr>=tm(1918m9))

	gen 	during_X_`v' 	= `v'*( (f_myr>=tm(1918m9)) & (f_myr<=tm(1919m3)) )
	gen 	after_X_`v' 	= `v'*(f_myr>=tm(1919m4))
	gen 	pre_X_`v' 		= `v'*(f_myr<=tm(1917m8))
	
	lab var post_X_`v'		"Post Pandemic $\times$ NPI Length"
	
	lab var pre_X_`v'		"Before Pandemic $\times$ NPI Length"
	lab var during_X_`v' 	"During Pandemic $\times$ NPI Length"
	lab var after_X_`v' 	"After Pandemic $\times$ NPI Length"
}

drop 	daysnpis 

sort gen_id f_myr 



*=======================*
*= Regression Models =*
*=======================*

est clear

** Combination of  Models

ppmlhdfe pat_wtd_inv post_X_longnpis if tin(1916m1, 1920m12), exp(pop_int) a(f_myr gen_id) cluster(gen_id)
ppmlhdfe pat_wtd_inv post_X_longnpis i.iparty if tin(1916m1, 1920m12), exp(pop_int) a(f_myr gen_id) cluster(gen_id)

ppmlhdfe pat_wtd_inv post_X_longnpis if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm regyr) cluster(gen_id)
ppmlhdfe pat_wtd_inv post_X_longnpis i.iparty if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm regyr) cluster(gen_id)

